import React from 'react'
import MenuConfig from '../../config/menuConfig.js'
import { Menu } from 'antd'
import { NavLink } from 'react-router-dom'
import './index.less'
const SubMenu = Menu.SubMenu
export default class NavLeft extends React.Component {
  state = {
    currentKey: ''
  }
  componentWillMount() {
    const menuTreeNode = this.renderMenu(MenuConfig)
    let currentKey = window.location.hash.replace(/#|\?.*$/g, '')
    this.setState({
      currentKey,
      menuTreeNode
    })
  }

  handleClick = ()=> {

  }

  // 菜单渲染
  renderMenu = (data) => {
    return data.map((item) => {
      if(item.children) {
        return (
          <SubMenu title={item.title} key={item.key}>
            {this.renderMenu(item.children)}
          </SubMenu>
        )
      }
      return <Menu.Item title={item.title} key={item.key}>
        <NavLink to={item.key}>{item.title}</NavLink>
      </Menu.Item>
    })
  }

  render() {
    return (
      <div>
        <div className="logo">
          <img src="/assets/logo-ant.svg" alt="logo" />
          <h1>OFO BACKEND</h1>
        </div>
        <Menu
          onClick={this.handleClick}
          selectedKeys={this.state.currentKey}
          theme="dark">
          { this.state.menuTreeNode }
        </Menu>
      </div>
    )
  }
}